using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Weazel.Toad.Application.Forms
{
  public partial class SynthesisOptionsForm : Weazel.Toad.Application.Forms.DialogBaseForm
  {
    public SynthesisOptionsForm()
    {
      InitializeComponent();

      // initialize content of "target" in "vhdl conversion" tab by calling
      vhdlConvRadioButtonsStateChanged(null, EventArgs.Empty);
    }

    protected override void OnClosing(CancelEventArgs e)
    {
      base.OnClosing(e);

      saveVhdlConversionSettings();
    }


    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      // populate the settings in the "vhdl conversion" tab using the settings
      // from the "settings" object of the GezelVhdlConversion object
      populateVhdlConversionTab();
    }

    private void populateVhdlConversionTab()
    {
      // get the settings object
      Items.GezelVhdlConversion.GezelVhdlConversionSettings settings =
        Application.Instance.GezelVhdlConversion.Settings;

      vhdlConvResetNameTextBox.Text = settings.ResetSignalName;
      vhdlConvClockNameTextBox.Text = settings.ClockAfterDivisionName;
      vhdlConvClockBeforeDivisionNameTextBox.Text = settings.ClockBeforeDivisionName;

      vhdlConvDivideClockCheckBox.Checked = settings.DivideClock;
      vhdlConvClockBeforeDivisionNameTextBox.Enabled = settings.DivideClock;

      switch (settings.ConversionTarget)
      {
        case Items.GezelVhdlConversion.GezelVhdlConversionSettings.GezelConversionTarget.Simulation:
          vhdlConvSimulationRadioButton.Checked = true;
          break;
        case Items.GezelVhdlConversion.GezelVhdlConversionSettings.GezelConversionTarget.Synthesis:
          vhdlConvSynthesisRadioButton.Checked = true;
          break;
        case Items.GezelVhdlConversion.GezelVhdlConversionSettings.GezelConversionTarget.Custom:
          vhdlConvCustomRadioButton.Checked = true;
          this.vhdlConvCreatePortsCheckBox.Checked = settings.CreatePortsForUnconnectedNets;
          this.vhdlConvConvertDisplayStatementsCheckBox.Checked = settings.ConvertDisplayDirectives;
          this.vhdlConvGenerateProcessesCheckBox.Checked = settings.CreateClockAndResetProcesses;
          break;
        default:
          throw new NotSupportedException();
      }
    }

    private void saveVhdlConversionSettings()
    {
      Items.GezelVhdlConversion.GezelVhdlConversionSettings settings =
        Application.Instance.GezelVhdlConversion.Settings;

      settings.DestinationFolder = vhdlConvDestinationFolder.Text;

      settings.ResetSignalName = vhdlConvResetNameTextBox.Text;
      settings.ClockBeforeDivisionName = vhdlConvClockBeforeDivisionNameTextBox.Text;
      settings.ClockAfterDivisionName = vhdlConvClockNameTextBox.Text;

      settings.ConversionTarget =
        vhdlConvSimulationRadioButton.Checked ? Items.GezelVhdlConversion.GezelVhdlConversionSettings.GezelConversionTarget.Simulation :
        vhdlConvSynthesisRadioButton.Checked ? Items.GezelVhdlConversion.GezelVhdlConversionSettings.GezelConversionTarget.Synthesis :
        Items.GezelVhdlConversion.GezelVhdlConversionSettings.GezelConversionTarget.Custom;

      settings.ConvertDisplayDirectives =
        vhdlConvConvertDisplayStatementsCheckBox.Checked;
      settings.CreatePortsForUnconnectedNets =
        vhdlConvCreatePortsCheckBox.Checked;
      settings.CreateClockAndResetProcesses =
        vhdlConvGenerateProcessesCheckBox.Checked;
    }

    private void vhdlConvBrowseButton_Click(object sender, EventArgs e)
    {
      System.Windows.Forms.FolderBrowserDialog dialog =
        new FolderBrowserDialog();

      if (System.IO.Directory.Exists(vhdlConvDestinationFolder.Text))
        dialog.SelectedPath = vhdlConvDestinationFolder.Text;

      if (dialog.ShowDialog() == DialogResult.OK)
      {
        vhdlConvDestinationFolder.Text = dialog.SelectedPath;
      }
    }

    private void vhdlConvRadioButtonsStateChanged(object sender, EventArgs e)
    {
      if (vhdlConvSynthesisRadioButton.Checked)
      {
        this.vhdlConvCreatePortsCheckBox.Enabled = false;
        this.vhdlConvConvertDisplayStatementsCheckBox.Enabled = false;
        this.vhdlConvGenerateProcessesCheckBox.Enabled = false;

        // reflect the choices ..
        this.vhdlConvCreatePortsCheckBox.Checked = true;
        this.vhdlConvConvertDisplayStatementsCheckBox.Checked = false;
        this.vhdlConvGenerateProcessesCheckBox.Checked = false;
      }
      else if (vhdlConvSimulationRadioButton.Checked)
      {
        this.vhdlConvCreatePortsCheckBox.Enabled = false;
        this.vhdlConvConvertDisplayStatementsCheckBox.Enabled = false;
        this.vhdlConvGenerateProcessesCheckBox.Enabled = false;

        // reflect the choices ..
        this.vhdlConvCreatePortsCheckBox.Checked = false;
        this.vhdlConvConvertDisplayStatementsCheckBox.Checked = true;
        this.vhdlConvGenerateProcessesCheckBox.Checked = true;
      }
      else if (vhdlConvCustomRadioButton.Checked)
      {
        this.vhdlConvCreatePortsCheckBox.Enabled = true;
        this.vhdlConvConvertDisplayStatementsCheckBox.Enabled = true;
        this.vhdlConvGenerateProcessesCheckBox.Enabled = true;

        // keep the values ..
      }
      else
      {
        throw new NotSupportedException();
      }
    }

    private void vhdlConvDivideClockCheckBox_CheckedChanged(object sender, EventArgs e)
    {
      if (vhdlConvDivideClockCheckBox.Checked)
        vhdlConvClockBeforeDivisionNameTextBox.Enabled = true;
      else
        vhdlConvClockBeforeDivisionNameTextBox.Enabled = false;
    } 
  }
}

